package com.mzj.saas.rent;

import com.haixiaoke.saas.repository.core.AjaxResult;
import com.haixiaoke.saas.repository.core.BaseController;
import com.haixiaoke.saas.repository.core.page.TableDataInfo;
import com.haixiaoke.saas.repository.pojo.ApartmentBillRule;
import com.haixiaoke.saas.repository.service.IApartmentBillRuleService;
import com.haixiaoke.saas.repository.vo.ApartmentBillRuleVo;
import com.mzj.saas.annotation.CheckAuth;
import com.mzj.saas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 账单生成规则Controller
 *
 * @author qixi
 * @date 2023-06-16
 */
@RestController
@RequestMapping("/business/rule")
public class ApartmentBillRuleController extends BaseController {
    @Autowired
    private IApartmentBillRuleService apartmentBillRuleService;

    @Autowired
    private LoginUserUtil loginUserUtil;

    /**
     * 查询账单生成规则列表
     */
    @GetMapping("/list")
    @CheckAuth
    public TableDataInfo list(ApartmentBillRule apartmentBillRule) {
        startPage();
        Long orgId = loginUserUtil.getOrgId();
        apartmentBillRule.setOrgId(orgId);
        List<ApartmentBillRuleVo> list = apartmentBillRuleService.selectApartmentBillRuleList(apartmentBillRule);
        return getDataTable(list);
    }


    /**
     * 获取账单生成规则详细信息
     */

    @GetMapping(value = "/{ruleId}")
    public AjaxResult getInfo(@PathVariable("ruleId") Long ruleId) {
        return success(apartmentBillRuleService.selectApartmentBillRuleByRuleId(ruleId));
    }

    /**
     * 新增账单生成规则
     */
    @PostMapping
    public AjaxResult add(@RequestBody ApartmentBillRule apartmentBillRule) {
        return toAjax(apartmentBillRuleService.insertApartmentBillRule(apartmentBillRule));
    }

    /**
     * 修改账单生成规则
     */
    @PutMapping
    @CheckAuth
    public AjaxResult edit(@RequestBody @Valid ApartmentBillRule apartmentBillRule) {
        String id = loginUserUtil.getId();
        apartmentBillRule.setUserId(id);
        return toAjax(apartmentBillRuleService.updateApartmentBillRuleAndRuleRef(apartmentBillRule));
    }

    /**
     * 删除账单生成规则
     */
    @DeleteMapping("/{ruleIds}")
    public AjaxResult remove(@PathVariable Long[] ruleIds) {
        return toAjax(apartmentBillRuleService.deleteApartmentBillRuleByRuleIds(ruleIds));
    }
}
